package com.be.beadmin.design.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.design.dto.input.StandProductProcessInfoDto;
import com.be.beadmin.design.dto.input.StandProductProcessInfoPageDto;
import com.be.beadmin.design.dto.output.StandProductProcessInfoEx;
import com.be.beadmin.design.dto.output.StandProductProcessInfoExcelEx;
import com.be.beadmin.design.service.StandProductProcessInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ljh
* @date 2023-06-13
**/
@Api(tags = {"设计管理-标准柜体工序工时"})
@RestController
public class StandProductProcessInfoController {

    @Autowired
    private  StandProductProcessInfoService standProductProcessInfoService;

    @PostMapping(value = "/design/getStandProductProcessInfoByPage")
    public ResultModel getStandProductProcessInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(standProductProcessInfoService.getStandProductProcessInfoByPage(pageEntity)) ;
    }

    @ApiOperation("标准柜体工序工时分页（根据柜体id)")
    @PostMapping(value = "/design/getStandProductProcessInfoByStandTypeIdAndPage")
    public ResultModel getStandProductProcessInfoByStandTypeIdAndPage(@RequestBody StandProductProcessInfoPageDto standProductProcessInfoPageDto)  {
        return ResultModel.success(standProductProcessInfoService.getStandProductProcessInfoByStandTypeIdAndPage(standProductProcessInfoPageDto)) ;
    }

    @PostMapping("/design/addStandProductProcessInfo")
    public ResultModel addStandProductProcessInfo(@RequestBody StandProductProcessInfoDto standProductProcessInfoDto){
        standProductProcessInfoService.addStandProductProcessInfo(standProductProcessInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/design/delStandProductProcessInfo")
    public ResultModel delStandProductProcessInfo(@RequestBody StandProductProcessInfoDto standProductProcessInfoDto){
        standProductProcessInfoService.delStandProductProcessInfo(standProductProcessInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("编辑工时")
    @PostMapping("/design/updateStandProductProcessInfo")
    public ResultModel updateStandProductProcessInfo(@RequestBody StandProductProcessInfoDto standProductProcessInfoDto){
        standProductProcessInfoService.updateStandProductProcessInfo(standProductProcessInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("导出多个标准柜体工时工期模板")
    @PostMapping("/design/exportStandProductProcessInfoByStandTypeIds")
    public void exportStandProductProcessInfo(HttpServletResponse response ,
                                              @ApiParam(value = "标准柜体ids", required = true) @RequestBody List<String> standTypeIds) throws IOException {

        List<StandProductProcessInfoExcelEx> collect = standProductProcessInfoService.getStandProductProcessInfoByStandTypeIds(standTypeIds);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("StandProductProcessInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), StandProductProcessInfoExcelEx.class).sheet("sheet1").doWrite(collect);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), StandProductProcessInfoExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(collect);
    }

    @ApiOperation("上传标准柜体工时工期文件")
    @PostMapping("/design/uploadStandProductProcessInfo")
    public ResultModel uploadStandProductProcessInfo(@RequestParam("file") MultipartFile[] files) {
        for(MultipartFile file:files) {
            if (file.isEmpty()) {
                throw new BeadminException("请上传文件!");
            }
            standProductProcessInfoService.uploadStandProductProcessInfo(file);
        }
        return ResultModel.success("上传成功");
    }
}